<?php
/**
 * Created by Yury Patrin.
 * Time: 04.12.13 15:59
 */

class Admin extends CI_Controller
{
    protected $activePage   = 'index';
    protected $content      = '';
    protected $user         = '';

    public function __construct()
    {
        parent::__construct();

        $this->load->library('session');
        $this->load->helper('url');

        $this->load->model('user_model');
        $this->load->model('group_model');

        $this->user = $this->session->userdata('user');

        $this->_checkLogin();
    }

    public function index()
    {
        $this->_buildPage();
    }

    public function users()
    {
        $this->activePage = "users";

        $users = $this->user_model->getUsersList();
        $groups = $this->group_model->getGroupsList();
        $this->content = $this->load->view('pages/users', ['users' => $users, 'groups' => $groups], true);

        $this->_buildPage();
    }

    public function groups()
    {
        $this->activePage = "groups";

        $groups = $this->group_model->getGroupsList();

        $this->content = $this->load->view('pages/groups', ['groups' => $groups], true);
        $this->_buildPage();
    }

    protected function _checkLogin()
    {
        $loginStatus = $this->session->userdata('login');

        if ($loginStatus != "true")
            redirect('index');
    }

    protected function _buildPage($params = [])
    {
        $params['activePage']   = $this->activePage;
        $params['content']      = $this->content;
        $params['user']         = $this->user;

        $this->load->view('admin_page', $params);
    }
}